var columns = [[
                {field:'name',title:'权限名称',width:50},
                {field:'permissionCode',title:'权限字符串',width:100},
                {field:'status',title:'状态',width:100,formatter: function (value, row, index) {
                    switch(value) {
                        case 1 : return '正常';
                        case 2 : return '停止';
                    }
                    return '未知';}}
			]];

$(document).ready(function () {

	$('#ff').form( {
		success : function(data) {
			data = eval('(' + data + ')');
			var iHeight = document.documentElement.clientHeight;
            $('#tt').datagrid( {
                width : '100%',
                height : iHeight-85,
                fitColumns : true,
                rownumbers:true,
                striped:true,
                columns : columns,
                singleSelect:true,//是否单选 
                toolbar: CreateToolBar(),
                pagination:true,
                pageList: [20,30,50,100],
                data:data.slice(0,20)
            });
            
            var pager = $("#tt").datagrid("getPager");  
            pager.pagination({  
                total:data.length,  
                onSelectPage:function (pageNo, pageSize) {  
                    var start = (pageNo - 1) * pageSize;  
                    var end = start + pageSize;  
                    $("#tt").datagrid("loadData", data.slice(start, end));  
                    pager.pagination('refresh', {  
                        total:data.length,  
                        pageNumber:pageNo  
                    });  
                }  
            });  
            
			try{window.parent.unmask();}catch(err){}
		}
	});
	$('#fmAdd').form( {
		success : function(data) {
		    CRUD.queryList();
		    try{
			    var result = eval('(' + data + ')');
			    if(result == true){
			    	$('#dialogAdd').dialog('close');
				 } else {
			        $.messager.alert('提示', '保存失败');
				 }
			}catch(err){
			}
		}
	});

    CRUD.queryList();
    InitForm();

    //确定添加
    $('#a_AddOK').click(function () {
        if (!document.getElementById("name").value) {
            $.messager.alert('提示', '权限名称不能为空！');
            return;
        }
        if (!$('#permissionCode').val()) {
            $.messager.alert('提示', '权限字符串不能为空！');
            return;
        }
        $('#fmAdd').submit();
    });

    //取消
    $('#a_AddCancle').click(function () {
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#fmAdd').form('clear');
        $('#dialogAdd').dialog('close');
        $('#tt').datagrid("clearSelections");
    });
});

var CRUD = {
    queryList: function () {
       try{window.parent.mask();}catch(err){}
	   $('#ff').submit();
    },

    Add: function () {
    	$('#fmAdd').form('clear');
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#dialogAdd').dialog('open');
        $('#tt').datagrid("clearSelections");
        $('#dialogAdd').dialog("setTitle", "添加权限");
    },
    Del: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r) {
            $.messager.confirm('删除提示', '你确认删除这一条数据吗？', function (d) {
                if (d) {
                    $.get(getContextPath() + "/manager/permission/delete.do?id=" + r.id, function (data) {
                        if (data == 'true') {
                            $.messager.show(
                                    {
                                        title: '系统提示',
                                        msg: '删除成功！',
                                        timeout: 1500
                                    });
                            $('#tt').datagrid("clearSelections");
                            CRUD.queryList();
                        } else {
                            $.messager.alert('错误', '删除失败！');
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', '请选择一行数据!');
        }
    },
    Update: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r == null) {
            $.messager.alert('提示', '请选择一行数据');
            return;
        }
        $('#dialogAdd').dialog('open');
        $('#fmAdd').form('load', r);
        if (r.ruleRegionType == 1) {
        	$('#scaleAll').attr('checked', 'checked');
        } else if (r.ruleRegionType == 2) {
        	$('#scaleUserDefined').attr('checked', 'checked');
        	var textarea = document.getElementById('idScales');
        	if (!textarea) {
        		$('#scaleTd').append($('<textarea style="display:block" id="idScales" cols ="50" rows = "3"></textarea>'));
        	}
        	$('#idScales').val(r.ruleRegion);
        }
        $('#dialogAdd').dialog("setTitle", "修改权限");
    }
};

function CreateToolBar() {
    var a = [];
    var isReflesh = true;
    var isAdd = true;
    var isDel = true;
    var isUpdate = true;
    if (isAdd) {
        a.push({
            text: '添加',
            iconCls: 'icon-add',
            handler: function () {
                CRUD.Add();
            }
        });
        a.push('-');
    }

    if (isUpdate) {
        a.push({
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                CRUD.Update();
            }
        });
        a.push('-');
    }

    if (isDel) {
        a.push({
            text: '删除',
            iconCls: 'icon-cut',
            handler: function () {
                CRUD.Del();
            }
        });
        a.push('-');
    }
    return a;
}


function InitForm() {
    var width = 420;
    var height = window.screen.height;
    if (height > 250) {
        height = 250;
    }
    $('#dialogAdd').dialog(
            {
                closed: true,
                title: "添加权限",
                width: width,
                height: height
            });
}


//各容器自适应
window.onresize = function () {
	var iHeight = document.documentElement.clientHeight;
	$("#tt").datagrid('resize',{height:iHeight-85+'px'});            
}